package com.example.lotterysystem.service.impl;

import com.example.lotterysystem.common.errorcode.ServiceErrorCodeConstants;
import com.example.lotterysystem.common.exception.ServiceException;
import com.example.lotterysystem.controller.param.CreatePrizeParam;
import com.example.lotterysystem.controller.param.PageParam;
import com.example.lotterysystem.dao.dataObject.ActivityPrizeDO;
import com.example.lotterysystem.dao.dataObject.PrizeDO;
import com.example.lotterysystem.dao.mapper.ActivityMapper;
import com.example.lotterysystem.dao.mapper.ActivityPrizeMapper;
import com.example.lotterysystem.dao.mapper.PrizeMapper;
import com.example.lotterysystem.service.PictureService;
import com.example.lotterysystem.service.PrizeService;
import com.example.lotterysystem.service.dto.PageListDTO;
import com.example.lotterysystem.service.dto.PrizeDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Service
public class PrizeServiceImpl implements PrizeService {
    private static final Logger logger = LoggerFactory.getLogger(PrizeServiceImpl.class);
    @Autowired
    private PictureService pictureService;
    @Autowired
    private PrizeMapper prizeMapper;
    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;
    @Autowired
    private ActivityMapper activityMapper;
    @Override
    public Long createPrize(CreatePrizeParam param, MultipartFile picFile) {
        //上传图片
        String str = pictureService.savePicture(picFile);
        //存库
        PrizeDO prizeDO = new PrizeDO();
        prizeDO.setName(param.getPrizeName());
        prizeDO.setDescription(param.getDescription());
        prizeDO.setImageUrl(str);
        prizeDO.setPrice(param.getPrice());
        prizeMapper.insert(prizeDO);
        return prizeDO.getId();
    }

    @Override
    public PageListDTO<PrizeDTO> findPrizeList(PageParam param) {
        //设置总量
        int total = prizeMapper.count();

        //查询当前页
        List<PrizeDTO> list = new ArrayList<>();
        List<PrizeDO> prizeDOList = prizeMapper.selectPrizeList(param.offset(),param.getPageSize());
        for(PrizeDO prizeDO: prizeDOList){
            PrizeDTO prizeDTO = new PrizeDTO();
            prizeDTO.setPrizeId(prizeDO.getId());
            prizeDTO.setName(prizeDO.getName());
            prizeDTO.setPrice(prizeDO.getPrice());
            prizeDTO.setDescription(prizeDO.getDescription());
            prizeDTO.setImageUrl(prizeDO.getImageUrl());
            list.add(prizeDTO);
        }
        return new PageListDTO<>(total,list);
    }

    @Override
    public Boolean deletePrize(Long prizeId) {
        Boolean result = checkPrize(prizeId);
        if (!result){
            logger.error("删除失败!活动或奖品正在进行抽奖!");
            throw new ServiceException(ServiceErrorCodeConstants.DELETE_PRIZE_FALSE);
        }
        prizeMapper.deletePrize(prizeId);
        return true;
    }

    private Boolean checkPrize(Long prizeId) {
        //先判断是否为空,如果为空就报错
        if(null==prizeId){
            logger.warn("选择删除的prizeId为空!");
            throw new ServiceException(ServiceErrorCodeConstants.DELETE_ID_IS_EMPTY);
        }
        //也要判断prizeId是否存在
        PrizeDO prizeDO = prizeMapper.selectById(prizeId);
        if(null==prizeDO){
            logger.error("选择删除的奖品不存在! prizeId:{}",prizeId);
            throw new ServiceException(ServiceErrorCodeConstants.DELETE_ID_IS_NOT_EXIST);
        }
        //通过奖品id进行查询,需要查两次活动关联奖品表
        //1.对活动关联奖品表进行查询,先找到活动id
        List<ActivityPrizeDO> activityPrizeDOS = activityPrizeMapper.selectByPrizeId(prizeId);
        //2.再查询一次,查奖品的状态,返回INIT的数量,为0就是现目前没有占位
        int prizeInit = activityPrizeMapper.countPrizeInit(prizeId,"INIT");//如果为0就代表所有被活动关联的奖品就都结束了

        //查询活动表
        //也是要返回的列表里面所有的活动状态为INIT的数量为0,就可以进行删除了
        int activityInit=0;
        if(!CollectionUtils.isEmpty(activityPrizeDOS)){
            activityInit = activityMapper.countActivityInitByPrize(activityPrizeDOS,"INIT");
        }
        //判断是否可以进行删除和更新
        if(prizeInit!=0||activityInit!=0){
            return false;
        }
        //表示可以进行删除更新
        return true;
    }
}
